<?php
namespace app\api\model;
use app\common\controller\Common;
use app\common\lib\RedisServer;
use app\api\model\Role as RoleModel;
use think\Db;
use app\api\model\Menu as MenuModel;

/**
 * @Author: Amor_L
 * @Date:   2020-06-05 14:03:24
 * @Last File:
 */
class Api extends Common
{
    protected $uid = 0;
    protected $style = 0;
    protected $user = [];

    public function initialize()
    {
        //不需要验证的方法
        $action_not_check_token = [
            'sendcode','login'
        ];
        //不需要验证的控制器
        $con_not_check_token = [
            'Userlogin','Toget',
        ];
        // 不需要验证的控制器加方法
        $con_action_check = [
            'api/User/index',
            'api/Index/index',

        ];

        //获取产品
        parent::initialize();
        //后台增加允许跨域访问
        header('Access-Control-Allow-Headers: content-type,authorization,flag');
        header('Access-Control-Allow-Methods: PUT, GET, POST, DELETE');
        header('Access-Control-Allow-Origin: *');
        header('Content-Type: application/json; charset=utf-8');
        
        $action = $this->request->action();
        $controller = $this->request->controller();
        $now = 'api/'.$controller.'/'.$action;
        // print_r($now);die;
        if(!in_array($controller,$con_not_check_token) && !in_array($action, $action_not_check_token)){
            $flag = input('server.HTTP_FLAG');
            if($flag){
                $uid = db('user_member')->where('flag',$flag)->where('type',1)->value('uid');
                if(!$uid){
                    echo json_encode(['code'=>'4','msg'=>'请登录']);die;
                }else{
                    $user = getuserinfo($uid);
                    if(!$user){
                        echo json_encode(['code'=>'1','msg'=>'请登录']);die;
                    }
                    if($user){
                        $this->user = $user;

                        // if($user['role']){
                        //     $role = explode(',',$user['role']);
                        //     $count = count($role);
                        //     if($count == 1){
                        //         $role = db::name('admin_role')->where('id',$user['role'])->find();
                        //         if($role['pid'] != 0){
                        //             $res = RoleModel::roleAuth($user['role']);
                        //             $str1 =str_replace("[","",$res[0]);
                        //             $str1 =str_replace("]","",$str1);
                        //             $newArr = explode(',',$str1);
                        //             $location = MenuModel::getLocation();
                        //             $action   = end($location);
                        //             if(!in_array($action['id'],$newArr)){
                        //                 echo json_encode(['code'=>'1','msg'=>'暂无权限']);die;
                        //             }
                        //             // return $url === false ? isset($menu_auth[$action['id']]) : in_array($action['url_value'], $res);
                        //         }
                        //         $this->user = $user;
                        //     }elseif($count > 1){
                        //         $roles = db::name('admin_role')->where('id','in',$user['role'])->select();
                        //         if($roles){
                        //             $menu = [];
                        //             foreach ($roles as $ke => $ve) {
                        //                 if($ve['pid'] != 0){
                        //                     $str1 =str_replace("[","",$ve['menu_auth']);
                        //                     $str1 =str_replace("]","",$str1);
                        //                     $newArr = explode(',',$str1);
                        //                     $menu = array_merge($newArr,$menu);
                        //                 }
                        //             }
                        //             $location = MenuModel::getLocation();
                        //             $action   = end($location);
                        //             if(!in_array($action['id'],$menu)){
                        //                 if(!in_array($now,$con_action_check)){
                        //                     echo json_encode(['code'=>'1','msg'=>'暂无权限']);die;
                        //                 }
                        //             }
                        //         }else{
                        //             echo json_encode(['code'=>'1','msg'=>'角色不存在']);die;
                        //         }
                        //     }
                        // }else{
                        //     echo json_encode(['code'=>'1','msg'=>'未分配权限']);die;

                        // }
                        // $this->user = $user;
                    }
                }
            }else{
                echo json_encode(['code'=>'1','msg'=>'请登录']);die;
            }
        }
    }
    //返回数据格式
    protected function ajaxReturn($code,$msg,$data = [])
    {
        $action = $this->request->action();
        if($this->uid){
            // @RedisServer::unlock($this->uid.'/'.request()->module().'/'.request()->controller().'/'.request()->action());
            if(!isset($data)){
                return json(['code'=>"$code",'msg'=>$msg]);
            }
        }
        return json(['code'=>"$code",'msg'=>$msg,'data'=>$data]);
    }


}
